/*******************************************************************************
*LAST EDITED: 06/30/2022
*CONTRIBUTORS: BC, MEK & AO
*PROJECT: Is It Who You Are or What You Get? Comparing the Impacts of Loans 
		  and Grants for Microenterprise Development
*JOURNAL: American Economic Journal-Applied Economics
*DESCRIPTION: This program builds all in-text Figures
*******************************************************************************/
***Set up 
	clear all
	set rmsg on
	set seed 12345
	set maxvar 32767  // Modify if not StataMP 
	set max_memory . // (.) operating system is willing to supply all memory
	set more off
	set matsize 10000
	set scheme s2color
	grstyle init
	grstyle set plain, horizontal grid
***Directories
	*Root 
		*global root "c\tmp\" // Include and update this line if you're running this do file individually and not through "00_SetUp.do"
	*Analysis data
		global adata "$root\Data"
	*Save figures
		global figs "$root\Output\Figures"
********************************************************************************

			***********************************************************
			***********************************************************
						      ***Figures in text***
			***********************************************************
			***********************************************************
/*
INPUT: This program requires 3 datasets 
	1) AEJ_CKO_baseline_edited.dta
	2) AEJ_CKO_endline_edited.dta
OUTPUT: The program generates Figure 1 & Figure 2 in paper 
*/			
	
	
******************************************************
***Figure 1. Capital Assistance Received Men and Women 
******************************************************
*For Women 
	*Call & prepare data
	use "$adata\AEJ_CKO_baseline_edited.dta",clear 
	*For Women 
		keep if b_baseline_gender==0
		gen treatment=b_treatment_status
		gen amount_received_corrected=b_amount_received_corrected
	*Drop controls
		drop if treatment==0
	*Vars of interest: amount_received_corrected 
		gen amount_bin=.
		replace amount_bin=1 if amount_received_corrected==0
		replace amount_bin=2 if amount_received_corrected>0 & amount_received_corrected<1750
		replace amount_bin=3 if amount_received_corrected>=1750 & amount_received_corrected<2250
		replace amount_bin=4 if amount_received_corrected>=2250 & amount_received_corrected<=2750
		replace amount_bin=5 if amount_received_corrected>2750 
	*Count 
		gen n=1
		bys treatment: egen total_treatment=total(n)
		bys treatment amount_bin: egen total_treatment_bin=total(n)
		gen percent=(total_treatment_bin/total_treatment)*100
		keep treatment amount_bin percent 
		duplicates drop 
	*Figure 
		sort treatment amount_bin
		gen order=_n
		replace order=order+1 if treatment==2
		replace order=order+2 if treatment==3
	
		twoway (bar percent order if amount_bin==1, color(navy) barw(.9)) ///
		(bar percent order if amount_bin==2, color(navy%80) barw(.9)) ///
		(bar percent order if amount_bin==3, color(navy%50) barw(.9)) /// 
		(bar percent order if amount_bin==4, barw(.9)) ///
		(bar percent order if amount_bin==5, barw(.9)), ///
		ytitle("Proportion of loans/grants") ///
		yscale(titlegap(*5)) xtitle("") ///
		xlabel(3 "Microcredit" 9 "In Kind Grant" 15 "Cash Grant") ///
		leg(r(1) order(1 "0" 2 "<1750" 3 "1750-2250" 4 "2250-2750" 5 ">2750") ///
		symysize(*.8)  symxsize(*.5) region(lc(white)) size(small))
		graph export "$figs\Main_Figures\Graph_CA_female.png", replace width(800)
		
*For Men 
	*Call & prepare data
	use "$adata\AEJ_CKO_baseline_edited.dta",clear 
	*For Men 
		keep if b_baseline_gender==1
		gen treatment=b_treatment_status
		gen amount_received_corrected=b_amount_received_corrected
	*Drop controls
		drop if treatment==0
	*Vars of interest: amount_received_corrected 
		gen amount_bin=.
		replace amount_bin=1 if amount_received_corrected==0
		replace amount_bin=2 if amount_received_corrected>0 & amount_received_corrected<1750
		replace amount_bin=3 if amount_received_corrected>=1750 & amount_received_corrected<2250
		replace amount_bin=4 if amount_received_corrected>=2250 & amount_received_corrected<=2750
		replace amount_bin=5 if amount_received_corrected>2750 
	*Count 
		gen n=1
		bys treatment: egen total_treatment=total(n)
		bys treatment amount_bin: egen total_treatment_bin=total(n)
		gen percent=(total_treatment_bin/total_treatment)*100
		keep treatment amount_bin percent 
		duplicates drop 
	*Figure 
		sort treatment amount_bin
		gen order=_n
		replace order=order+1 if treatment==2
		replace order=order+2 if treatment==3
	
		twoway (bar percent order if amount_bin==1, color(navy) barw(.9)) ///
		(bar percent order if amount_bin==2, color(navy%80) barw(.9)) ///
		(bar percent order if amount_bin==3, color(navy%50) barw(.9)) /// 
		(bar percent order if amount_bin==4, barw(.9)) ///
		(bar percent order if amount_bin==5, barw(.9)), ///
		ytitle("Proportion of loans/grants") ///
		yscale(titlegap(*5)) xtitle("") ///
		xlabel(3 "Microcredit" 9 "In Kind Grant" 15 "Cash Grant") ///
		leg(r(1) order(1 "0" 2 "<1750" 3 "1750-2250" 4 "2250-2750" 5 ">2750") ///
		symysize(*.8)  symxsize(*.5) region(lc(white)) size(small))
		graph export "$figs\Main_Figures\Graph_CA_male.png", replace width(800)
		

********************************************************
***Figure 2. Quantile Treatment Effects for Total Income
********************************************************

***FOR MEN 
	*Call data
		use "$adata\AEJ_CKO_endline_edited.dta",clear 
		*Quantile Regressions 
			ivqte labor_income (Loan) if treatment!=2 &  treatment!=3 & male==1, ///
			quantiles(0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 0.975) ///
			variance unordered(b_round)
			matrix b1=e(b)'
			matrix v=vecdiag(e(V))'
			matrix std1=vecdiag(cholesky(diag(v)))'
			matrix q1=e(quantiles)'
			matrix list q1 v std1

			ivqte labor_income (In_Kind) if treatment!=1 &  treatment!=3 & male==1, ///
			quantiles(0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 0.975) ///
			variance unordered(b_round)
			matrix b2=e(b)'
			matrix v=vecdiag(e(V))'
			matrix std2=vecdiag(cholesky(diag(v)))'
			matrix q2=e(quantiles)'
			matrix list q2 v std2

			ivqte labor_income (Cash) if treatment!=2 &  treatment!=1 & male==1, ///
			quantiles(0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 0.975) ///
			variance unordered(b_round)
			matrix b3=e(b)'
			matrix v=vecdiag(e(V))'
			matrix std3=vecdiag(cholesky(diag(v)))'
			matrix q3=e(quantiles)'
			matrix list q3 v std3

		*Retrieve and format results 
			matrix M=(q1,b1,std1,b2,std2,b3,std3)
			matrix list M
			*Format 
				clear
				svmat M
				g Loan_H = M2 + 1.96*M3
				g Loan_L = M2 - 1.96*M3
				g In_Kind_H = M4 + 1.96*M5
				g In_Kind_L = M4 - 1.96*M5
				g Cash_H = M6 + 1.96*M7
				g Cash_L = M6 - 1.96*M7
				rename M1 q
				rename M2 Loan
				rename M4 In_Kind
				rename M6 Cash
				matrix list M
			*Censor for aesthetics 
				foreach e in Loan_H Loan_L In_Kind_H In_Kind_L Cash_H Cash_L{
					replace `e'=2000 if `e'>=2000
					replace `e'=-600 if `e'<=-600
				}
		*Figures 
			twoway (rarea Loan_H Loan_L q, color(blue*0.2)) ///
			(connected Loan q, color(blue)  lpattern(dash) msize(vsmall))  , yline(0) ///
			legend( label(2 "Quantile Treatment Effect Loan - Male") label(1 "95% CI") order(1 2) position(7) r(1))  ///
			xscale(range(0 1)) ///
			xlabel(0.0[0.1]1, nogrid) ///
			yscale(range(-600 2000)) ///
			ylabel(-600[200]2000, angle(0) nogrid) ///
			ytitle(`var2') ///
			xtitle(Quantile Treatment Effect Loan - income)
			*Export 
				graph export "$figs/Main_Figures/EGraph_QTE_Loan(Male).png", replace width(800)

			twoway (rarea In_Kind_H In_Kind_L q, color(blue*0.2)) ///
			(connected In_Kind q, color(blue)  lpattern(dash) msize(vsmall))  , yline(0) ///
			legend( label(2 "Quantile Treatment Effect In-Kind Grant - Male") label(1 "95% CI") order(1 2) position(7) r(1))  ///
			xscale(range(0 1)) ///
			xlabel(0.0[0.1]1, nogrid) ///
			yscale(range(-600 2000)) ///
			ylabel(-600[200]2000, angle(0) nogrid) ///
			ytitle(`var2') ///
			xtitle(Quantile Treatment Effect Loan - income)
			*Export 
				graph export "$figs/Main_Figures/EGraph_QTE_In_Kind(Male).png", replace width(800)

			twoway (rarea Cash_H Cash_L q, color(blue*0.2)) ///
			(connected Cash q, color(blue)  lpattern(dash) msize(vsmall))  , yline(0) ///
			legend( label(2 "Quantile Treatment Effect Cash Grant - Male") label(1 "95% CI") order(1 2) position(7) r(1))  ///
			xscale(range(0 1)) ///
			xlabel(0.0[0.1]1, nogrid) ///
			yscale(range(-600 2000)) ///
			ylabel(-600[200]2000, angle(0) nogrid) ///
			ytitle(`var2') ///
			xtitle(Quantile Treatment Effect Loan - income)
			*Export 
				graph export "$figs/Main_Figures/EGraph_QTE_Cash(Male).png", replace width(800)

***FOR WOMEN 
	*Call data
		use "$adata\AEJ_CKO_endline_edited.dta",clear 
		*Quantile Regressions 
			ivqte labor_income (Loan ) if treatment!=2 &  treatment!=3 & male==0, ///
			quantiles(0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 0.975) ///
			variance unordered(b_round)
			matrix b1=e(b)'
			matrix v=vecdiag(e(V))'
			matrix std1=vecdiag(cholesky(diag(v)))'
			matrix q1=e(quantiles)'
			matrix list q1 v std1

			ivqte labor_income (In_Kind) if treatment!=1 &  treatment!=3 & male==0, ///
			quantiles(0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 0.975) ///
			variance unordered(b_round)
			matrix b2=e(b)'
			matrix v=vecdiag(e(V))'
			matrix std2=vecdiag(cholesky(diag(v)))'
			matrix q2=e(quantiles)'
			matrix list q2 v std2

			ivqte labor_income (Cash) if treatment!=2 &  treatment!=1 & male==0, ///
			quantiles(0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 0.975) ///
			variance unordered(b_round)
			matrix b3=e(b)'
			matrix v=vecdiag(e(V))'
			matrix std3=vecdiag(cholesky(diag(v)))'
			matrix q3=e(quantiles)'
			matrix list q3 v std3

		*Retrieve and format results 
				matrix M=(q1,b1,std1,b2,std2,b3,std3)
				matrix list M
				*Format 
					clear
					svmat M
					g Loan_H = M2 + 1.96*M3
					g Loan_L = M2 - 1.96*M3
					g In_Kind_H = M4 + 1.96*M5
					g In_Kind_L = M4 - 1.96*M5
					g Cash_H = M6 + 1.96*M7
					g Cash_L = M6 - 1.96*M7
					rename M1 q
					rename M2 Loan
					rename M4 In_Kind
					rename M6 Cash
					matrix list M
				*Censor for aesthetics 
					foreach e in Loan_H Loan_L In_Kind_H In_Kind_L Cash_H Cash_L{
						replace `e'=2000 if `e'>=2000
						replace `e'=-600 if `e'<=-600
					}
		*Figures 
			twoway (rarea Loan_H Loan_L q, color(blue*0.2)) ///
			(connected Loan q, color(blue)  lpattern(dash) msize(vsmall))  , yline(0) ///
			legend( label(2 "Quantile Treatment Effect Loan - Female") label(1 "95% CI") order(1 2) position(7) r(1))  ///
			xscale(range(0 1)) ///
			xlabel(0.0[0.1]1, nogrid) ///
			yscale(range(-600 2200)) ///
			ylabel(-600[200]2200, angle(0) nogrid) ///
			ytitle(`var2') ///
			xtitle(Quantile Treatment Effect Loan - income)
				*Export 
					graph export "$figs/Main_Figures/Graph_QTE_Loan(All).png", replace width(800)

			twoway (rarea In_Kind_H In_Kind_L q, color(blue*0.2)) ///
			(connected In_Kind q, color(blue)  lpattern(dash) msize(vsmall))  , yline(0) ///
			legend( label(2 "Quantile Treatment Effect In-Kind Grant - Female") label(1 "95% CI") order(1 2) position(7) r(1))  ///
			xscale(range(0 1)) ///
			xlabel(0.0[0.1]1, nogrid) ///
			yscale(range(-600 2200)) ///
			ylabel(-600[200]2200, angle(0) nogrid) ///
			ytitle(`var2') ///
			xtitle(Quantile Treatment Effect Loan - income)
				*Export
					graph export "$figs/Main_Figures/Graph_QTE_In_Kind(All).png", replace width(800)

			twoway (rarea Cash_H Cash_L q, color(blue*0.2)) ///
			(connected Cash q, color(blue)  lpattern(dash) msize(vsmall))  , yline(0) ///
			legend( label(2 "Quantile Treatment Effect Cash Grant - Female") label(1 "95% CI") order(1 2) position(7) r(1))  ///
			xscale(range(0 1)) ///
			xlabel(0.0[0.1]1 , nogrid) ///
			yscale(range(-600 2200)) ///
			ylabel(-600[200]2200, angle(0) nogrid) ///
			ytitle(`var2') ///
			xtitle(Quantile Treatment Effect Loan - income)
				*Export 
					graph export "$figs/Main_Figures/Graph_QTE_Cash(All).png", replace width(800)

			   
